import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        javaPersonCount: 0,
        pythonPersonCount: 0,

        users: [
            {id: '1', name: '张三', age: '18', gender: 0},
            {id: '2', name: '张四', age: '28', gender: 1},
            {id: '3', name: '张五', age: '38', gender: 0},
            {id: '4', name: '张六', age: '48', gender: 1},
            {id: '5', name: '张七', age: '58', gender: 0},
        ]
    },
    mutations: {
        incrementJavaPersonCount(state) {
            state.javaPersonCount++
        },
        incrementPythonPersonCount (state) {
            state.pythonPersonCount++
        },
    },
    getters: {
        maleList: state => {
            return state.users.filter(user => user.gender)
        },
        femaleList: state => {
            return state.users.filter(user => !user.gender)
        },
        // 通过让 getter 返回一个函数，来实现给 getter 传参
        olderThan: (state) => (age) => {
            return state.users.filter(user => user.age > age)
        }
    }
})

export default store